package com.csxy.lsh.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.csxy.lsh.exception.MyException;
import com.csxy.lsh.service.VodService;
import com.csxy.lsh.util.ConstantVodUtils;
import com.csxy.lsh.util.InitObject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.lang3.StringUtils;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideoAliyun(MultipartFile file) {
        String fileName = file.getOriginalFilename();

        String title = fileName.substring(0,fileName.lastIndexOf("."));



        try {
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantVodUtils.ACCESSKEY_ID,
                   ConstantVodUtils.ACCESSKEY_SECRET,
                    title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.println("requestId=" + response.getRequestId() + "\n");//请求视频点播服务的请求Id
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
                System.out.println("videoId=" + getPlayUrl(videoId));
            } else {//如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空
                videoId = response.getVideoId();
                System.out.println("videoId=" + getPlayUrl(videoId));
            }
            return videoId;
        } catch (IOException e){
            throw  new MyException(20001,"上传失败");
        }


    }
//根据id删除
    @Override
    public void removeAliyunVideoById(String id) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            DeleteVideoResponse response = client.getAcsResponse(request);
        } catch (Exception e){
            throw new MyException(20001,"删除失败");
        }
    }
//批量删除
    @Override
    public void removeMoreVideo(List<String> videoIdList) {

        String str= StringUtils.join(videoIdList, ",");
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);
            DeleteVideoResponse response = client.getAcsResponse(request);
        }catch (Exception e){
            throw new MyException(20001,"批量删除失败");
        }
    }

    public String getPlayUrl(String videoId){
        try{
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(videoId);
            GetPlayInfoResponse response = client.getAcsResponse(request);
            return response.getPlayInfoList().get(0).getPlayURL();
        }catch (Exception e){
            e.printStackTrace(); // 打印详细异常信息到控制台
            throw new MyException(20001,"获取播放地址失败: " + e.getMessage());
        }
    }
}
